package com.android.gardensnake;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

/**
 * Very simple dynamical layout example
 * */

public class BasicDynamicalLayout extends Activity{
	
	RelativeLayout rl;
	TextView TVtotal;
	Button Badd, Bsub, Badd101;
	int sum;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		sum = 0;
		
		rl = new RelativeLayout(BasicDynamicalLayout.this); //or just "rl = new RelativeLayout(this);"
		rl.setBackgroundColor(Color.rgb(0, 0, 0)); //Black
		
		TVtotal = new TextView(this);
		TVtotal.setText("Total: " + sum);
		TVtotal.setTextColor(Color.rgb(255, 255, 255)); //White
		TVtotal.setTextSize(20.0f);
		TVtotal.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
		TVtotal.setId(1);
		LayoutParams paramsTVtotal = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		paramsTVtotal.topMargin = 5;
		
		Badd = new Button(this);
		Badd.setText("Add One");
		Badd.setTextColor(Color.WHITE);
		Badd.setTextSize(10.0f);
		Badd.setId(2);
		LayoutParams paramsBadd = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		paramsBadd.addRule(RelativeLayout.BELOW, TVtotal.getId());
		paramsBadd.topMargin = 50;

		
		Bsub = new Button(this);
		Bsub.setText("Subtract One");
		Bsub.setTextColor(Color.WHITE);
		Bsub.setTextSize(10.0f);
		Bsub.setId(3);
		LayoutParams paramsBsub = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		paramsBsub.addRule(RelativeLayout.BELOW, Badd.getId());
		paramsBsub.topMargin = 10;
		
		Badd101 = new Button(this);
		Badd101.setText("Add a hundred and one");
		Badd101.setTextColor(Color.WHITE);
		Badd101.setTextSize(10.0f);
		Badd101.setId(4);
		LayoutParams paramsBadd101 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		paramsBadd101.addRule(RelativeLayout.BELOW, Bsub.getId());
		paramsBadd101.topMargin = 10;

		rl.addView(TVtotal, paramsTVtotal);
		rl.addView(Badd, paramsBadd);
		rl.addView(Bsub, paramsBsub);
		rl.addView(Badd101, paramsBadd101); 
		
		setContentView(rl);
		
		thelistener();
	}

	private void thelistener() {
		// TODO Auto-generated method stub
		
		Badd.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sum++;
				changetheTV();
				
			}
		});
		
		Bsub.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sum--;
				changetheTV();
			}
		});
		
		Badd101.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				sum+=101;
				changetheTV();
				
			}
		});
		
	}

	protected void changetheTV() {
		// TODO Auto-generated method stub
		TVtotal.setText("Total: " + sum);
	}

	
}
